// JavaScript Document
function getAllElementsAsJson(parent, elements)
{
	var domtree = "[";
	var kids = $(parent).children(elements);
	var lenght = (kids.length);
	$.each(kids, function(i, data){
						  	if(i == (lenght-1)){
							  	domtree += "\""+$(kids).attr("id")+"\"";
							}else{
								domtree += "\""+$(kids).attr("id")+"\",";
							}
						  });
	domtree += "]";
	return domtree;
}

function parseFormularAsJson(formular)
{
	var json = "{";
	var submitstat;
	$.each(formular, function(i, data){
							  	switch(data.name){
									case 'parent':
										json += "\"parent\" : "+data.value+",";
										break;
									case 'id':
										json += "\"id\" : "+data.value+",";
										break;
									case 'title':
										json += "\"title\" : \""+data.value+"\",";
										break;
									case 'user':
										json += "\"user\" : \""+data.value+"\",";
										break;
									case 'date':
										json += "\"datetime\" : \""+data.value+"\",";
										break;
									case 'published':
										json += "\"published\" : \""+data.value+"\",";
										break;
									case 'submit':
										submitstat = data.value;
										break;
								}
							  });
	json += "\"content\" : "+getAllElementsAsJson($("#content-textarea"), "img");
	json += "}";
	$.post("/admin/site/do", {submit: submitstat, data:json}, function(data, status){
																	   if(data == "success")
																	   {
																		   $("p#status-signal").html("Seite erfolgreich gespeichert!");
																	   }else {
																		    $("p#status-signal").html(data);
																	   }
							  });
	
}
$(document).ready(function(){
	$("#site-edit").submit(function(){
								  parseFormularAsJson(this);
								  return false;
								  });					   
});
